package com.qf.time;

import com.qf.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 定时任务类 - 定时处理酒店的点击率问题
 */
@Component
public class ClicksTimeTask {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private ISearchService searchService;

    /**
     * 每10秒同步一次点击率到es中
     */
    @Scheduled(cron = "0/10 * * * * *")
    public void timeTask(){

        //从哈希结构中获得酒店点击率的信息
        Map<Object, Object> hotalCLicks = redisTemplate.opsForHash().entries("hotal_clicks");

        //循环处理
        for (Map.Entry<Object, Object> entry : hotalCLicks.entrySet()) {
            //酒店ID
            Integer hid = Integer.valueOf((String) entry.getKey());
            //点击率
            Integer clicks = Integer.valueOf((String) entry.getValue());

            //更新ES中酒店的点击率信息
            searchService.updateHotalClicks(hid, clicks);
        }

        //清空redis中的数据库
        redisTemplate.delete("hotal_clicks");
    }
}
